import { message } from "@/utils/message";
import { queryProduceExamByAll } from "@/api/produceexam";
import { type PaginationProps } from "@pureadmin/table";
import { reactive, ref, onMounted } from "vue";

export function useRole() {
  const dataList = ref([]);
  const loading = ref(true);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const columns: TableColumnList = [
    {
      label: "考试名称",
      prop: "taskName",
      minWidth: 140
    },
    {
      label: "考试时间起",
      prop: "starttime",
      minWidth: 130
    },
    {
      label: "考试时间止",
      prop: "endtime",
      minWidth: 130
    },
    {
      label: "考试总时长",
      prop: "times",
      minWidth: 80,
      formatter: row => {
        return `${row.times}(分钟)`;
      }
    },
    {
      label: "试卷总分数",
      prop: "totalscore",
      minWidth: 80,
      formatter: row => {
        return `${row.totalscore}分`;
      }
    },
    {
      label: "试卷得分",
      prop: "score",
      minWidth: 80,
      cellRenderer: ({ row }) => <el-tag type="danger">{row.score}分</el-tag>
    }
  ];

  function handleSizeChange(val: number) {
    console.log(`${val} items per page`);
  }

  function handleCurrentChange(val: number) {
    console.log(`current page: ${val}`);
    onSearch();
  }

  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
  }

  async function onSearch() {
    loading.value = true;
    queryProduceExamByAll({
      pageSize: pagination.pageSize,
      currentPage: pagination.currentPage
    })
      .then(res => {
        if (res.success) {
          dataList.value = res.data.list;
          pagination.total = res.data.total;
          loading.value = false;
        } else {
          message("查询数据异常:" + res.errMsg, { type: "error" });
        }
      })
      .catch(error => {
        message("查询数据异常:" + JSON.stringify(error), { type: "error" });
      });
  }

  onMounted(() => {
    onSearch();
  });

  return {
    loading,
    columns,
    dataList,
    pagination,
    onSearch,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange
  };
}
